package com.huike.web.controller.contract;

import com.huike.common.constant.ErrorMessageConstants;
import com.huike.common.constant.MagicConstants;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import com.huike.contract.domain.dto.GetTransferListDTO;
import com.huike.contract.domain.vo.TransferVo;
import com.huike.contract.service.TransferService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description 转派相关请求
 * @Date 2023-10-10
 */
@RequestMapping("/transfer")
@RequiredArgsConstructor
@RestController
@Slf4j
public class TransferController {

    private final TransferService transferService;

    /**
     * 获取转派列表
     *
     * @param dto 筛选条件
     */
    @GetMapping("/list")
    public TableDataInfo<List<TransferVo>> getTransferList(GetTransferListDTO dto) {
        if (dto.getParams() != null && dto.getParams().get(MagicConstants.BEGIN_TIME) != null)
            dto.setBeginTime((String) dto.getParams().get(MagicConstants.BEGIN_TIME));
        if (dto.getParams() != null && dto.getParams().get(MagicConstants.END_TIME) != null)
            dto.setEndTime((String) dto.getParams().get(MagicConstants.END_TIME));
        log.info("Get dto: {}", dto);
        return transferService.getTransferList(dto);
    }

    /**
     * 处理转派
     *
     * @param type           转派类型：0-线索，1-商机
     * @param userId         从哪个用户转派出去
     * @param transferUserId 转派给哪个用户
     * @return flag-转派处理结果是否成功，msg-失败消息
     */
    @PutMapping("/assignment/{type}/{userId}/{transferUserId}")
    public AjaxResult<Map<String, Object>> transfer(
            @PathVariable(name = "type") Integer type,
            @PathVariable(name = "userId") Long userId,
            @PathVariable(name = "transferUserId") Long transferUserId
    ) {
        log.info("Get type: {}, userId: {}, transferUserId: {}", type, userId, transferUserId);
        if (type == null || userId == null || transferUserId == null) {
            throw new BaseException(ErrorMessageConstants.TRANSFER_PARAMS_EXCEPTION);
        }
        return AjaxResult.success(transferService.transfer(type, userId, transferUserId));
    }

}
